package org.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.example.domain.dto.PageDTO;
import org.example.domain.po.ErpDepotHead;
import org.example.domain.query.report.*;
import org.example.domain.po.ErpUser;
import org.example.domain.vo.report.account.AccountBillVO;
import org.example.domain.vo.report.account.BaseAccountHead;
import org.example.domain.vo.report.account.ErpAccountVO;
import org.example.domain.vo.report.allocate.AllocateDepot;
import org.example.domain.vo.report.inDetail.Menyimpan;
import org.example.domain.vo.report.inOutStockReport.InOutStockReport;
import org.example.domain.vo.report.materialStock.ErpMaterialStockVO;
import org.example.domain.vo.report.materialStock.InventoryFlows;
import org.example.domain.vo.report.materialStock.billDetail.BaseInventory;
import org.example.domain.vo.report.retail.RetailStatistics;
import org.example.domain.vo.report.stock_warning.StockWarningReport;
import org.example.domain.vo.report.summary.StorageSummary;
import org.example.service.ErpDepotService;
import org.example.service.ErpReportService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/report")
@RestController
public class ErpReportController {
    @Autowired
    private ErpReportService reportService;
    
    @GetMapping("/findMaterialStock")
    @ApiOperation("商品库存查询")
    public Result<PageDTO<ErpMaterialStockVO>> findMaterialStock(@RequestAttribute("user") ErpUser user, StockQuery stockQuery){
        stockQuery.setTenantId(user.getTenantId());
        return Result.success(reportService.findMaterialStock(stockQuery));
    }
    
    @GetMapping("/findMaterialBill")
    @ApiOperation("查询商品库存流水")
    public Result<IPage<InventoryFlows>> findMaterialBill(@RequestAttribute("user") ErpUser user, MaterialBillQuery materialBillQuery){
        materialBillQuery.setTenantId(user.getTenantId());
        IPage<InventoryFlows> iPage = reportService.findMaterialBill(materialBillQuery);
        return Result.success(iPage);
    }
    
    @GetMapping("/getDeportHead")
    @ApiOperation("根据订单编号查询订单主表信息")
    public Result<ErpDepotHead> getDeportHead(String headNumber){
        ErpDepotHead deportHeadByNumber = reportService.getDeportHeadByNumber(headNumber);
        return Result.success(deportHeadByNumber);
    }
    
    @GetMapping("/getDetail")
    @ApiOperation("根据订单编号查询商品库存流水详情")
    public Result<BaseInventory> getDetail(String number){
        return Result.success(reportService.getDetail(number));
    }
    
    @GetMapping("/pageFindAccount")
    @ApiOperation("条件查询查询租户名下账户")
    public Result<IPage<ErpAccountVO>> pageFindAccount(@RequestAttribute("user") ErpUser user, AccQuery accQuery){
        accQuery.setTenantId(user.getTenantId());
        return Result.success(reportService.pageFindAccount(accQuery));
    }

    @GetMapping("/findAccountBill")
    @ApiOperation("查询该账户下的流水")
    public Result<List<AccountBillVO>> findAccountBill(@RequestAttribute("user") ErpUser user, Long accId){
        return Result.success(reportService.findAccountBill(accId, user.getTenantId()));
    }
    
    @GetMapping("/getAccountHeadDetail")
    @ApiOperation("查询账单详情信息")
    public Result<BaseAccountHead> getAccountHeadDetail(@RequestAttribute("user") ErpUser user, String headNumber, String subType){
        return Result.success(reportService.getAccountHeadDetail(user.getTenantId(), headNumber, subType));
    }
    
    @GetMapping("/findRetailStatistics")
    @ApiModelProperty("查询零售报表信息")
    public Result<IPage<RetailStatistics>> findRetailStatistics(@RequestAttribute("user") ErpUser user, RetailStatisticsQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findRetailStatistics(query));
    }
    
    @GetMapping("/findPurchaseStatistics")
    @ApiModelProperty("查询采购报表信息")
    public Result<IPage<RetailStatistics>> findPurchaseStatistics(@RequestAttribute("user") ErpUser user, RetailStatisticsQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findPurchaseStatistics(query));
    }
    
    @GetMapping("/findSaleStatistics")
    @ApiModelProperty("查询销售报表信息")
    public Result<IPage<RetailStatistics>> findSaleStatistics(@RequestAttribute("user") ErpUser user, RetailStatisticsQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findSaleStatistics(query));
    }
    
    @GetMapping("/findMenyimpanPage")
    @ApiModelProperty("查询入/出库明细报表")
    public Result<IPage<Menyimpan>> findMenyimpanPage(@RequestAttribute("user") ErpUser user, InDetailQuery inDetailQuery){
        inDetailQuery.setTenantId(user.getTenantId());
        return Result.success(reportService.findMenyimpanPage(inDetailQuery));
    }
    
    @GetMapping("/findStorageSummaryPage")
    @ApiModelProperty("查询入/出库汇总报表")
    public Result<IPage<StorageSummary>> findStorageSummaryPage(@RequestAttribute("user") ErpUser user, StorageSummaryQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findStorageSummaryPage(query));
    }
    
    @GetMapping("/findAllocateDepotPage")
    @ApiModelProperty("调拨明细报表")
    public Result<IPage<AllocateDepot>> findAllocateDepotPage(@RequestAttribute("user") ErpUser user, AllocateDepotQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findAllocateDepotPage(query));
    }
    
    @GetMapping("/findInOutStockReportPage")
    @ApiModelProperty("进销存统计报表")
    public Result<IPage<InOutStockReport>> findInOutStockReportPage(@RequestAttribute("user") ErpUser user, InOutStockReportQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findInOutStockReportPage(query));
    }
    
    @GetMapping("/findStockWarning")
    @ApiOperation("")
    public Result<IPage<StockWarningReport>> findStockWarning(@RequestAttribute("user") ErpUser user, InOutStockReportQuery query){
        query.setTenantId(user.getTenantId());
        return Result.success(reportService.findStockWarning(query));
    }
}
